package com.edu.stress.test.controller;

import com.edu.stress.test.constant.Constant;
import com.edu.stress.test.service.CheckService;
import com.edu.stress.test.service.DownService;
import com.edu.stress.test.service.RegisterService;
import com.edu.stress.test.service.UpgradeService;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.log4j.Logger;

/**
 * register+checkVersion+DownloadReport+UpgradeReport
 */
public class UpgradeController extends AbstractJavaSamplerClient {
    private static final Logger logger = Logger.getLogger(UpgradeController.class);

    public Arguments getDefaultParameters() {
        Arguments params = new Arguments();
        params.addArgument("vin", "");
        return params;
    }


    public SampleResult runTest(JavaSamplerContext context) {

        String vin = context.getParameter("vin");

        SampleResult sr = new SampleResult();
        sr.setSampleLabel("升级上报接口");
        sr.sampleStart();

        Integer beforeReg = Constant.RANDOM.nextInt(Constant.GAP);
        Integer beforeCheck = Constant.RANDOM.nextInt(Constant.GAP);
        Integer beforeDown = Constant.RANDOM.nextInt(Constant.GAP);
        Integer beforeUpgrade = Constant.RANDOM.nextInt(Constant.GAP);
        logger.info("vin:"+vin+"。sleep:"+beforeReg+"ms后调用register接口;sleep: "+ beforeCheck +" ms后调用check接口;sleep: "+ beforeDown +" ms后调用down接口;sleep: "+ beforeUpgrade +" ms后调用up接口。");


        try {
            RegisterService register = new RegisterService(vin);
            Thread.sleep(beforeReg);
            if (register.register()) {
                CheckService check = new CheckService(register.getDeviceId(), register.getSecret());
                Thread.sleep(beforeCheck);
                if (check.check()) {
                    DownService down = new DownService(register.getDeviceId(), register.getSecret(), check.getSessionId());
                    Thread.sleep(beforeDown);
                    if (down.down()) {

                        UpgradeService upgrade = new UpgradeService(register.getDeviceId(), register.getSecret(), check.getSessionId());
                        Thread.sleep(beforeUpgrade);
                        if (upgrade.upgrade()) {
                            sr.setResponseData("response:" + down.getResponse(), null);
                            sr.setDataType(SampleResult.TEXT);
                            sr.setResponseMessage(vin);
                            sr.setResponseCodeOK();
                            sr.setSuccessful(true);
                        } else {
                            logger.info("vin:"+vin+"升级失败");
                        }

                    } else {
                        logger.info("vin:"+vin+"下载失败");
                    }

                } else {
                    logger.info("vin:"+vin+"检测失败");
                }

            } else {
                logger.info("vin:"+vin+"注册失败");
            }

        } catch (Exception e) {
            sr.setSuccessful(false);
            e.printStackTrace();
        } finally {
            sr.sampleEnd();
        }
        return sr;
    }
    public static void main(String[] args) {
        Arguments params = new Arguments();
        params.addArgument("vin", "wxlStress00000001");
        JavaSamplerContext context = new JavaSamplerContext(params);
        UpgradeController test = new UpgradeController();
        test.setupTest(context);
        test.runTest(context);
        test.teardownTest(context);

    }
}
